package com.item.service;

import com.base.MYjyConstant;
import com.base.util.StringUtil;
import com.item.ConstantsCode;
import com.item.SystemConst;
import com.item.dao.CodeMapper;
import com.item.dao.model.Code;
import com.item.dao.model.CodeExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CodeService {
    @Autowired
    private CodeMapper codeMapper;

    private Map<String,Code> codeMap;

    public int countByExample(CodeExample example) {
        return this.codeMapper.countByExample(example);
    }

    public Code selectByPrimaryKey(String code) {
        return this.codeMapper.selectByPrimaryKey(code);
    }

    public List<Code> selectByExample(CodeExample example) {
        return this.codeMapper.selectByExample(example);
    }

    public int deleteByPrimaryKey(String code) {
        codeMap = null;
        return codeMapper.deleteByPrimaryKey(code);
    }

    public int updateByPrimaryKeySelective(Code record) {
        codeMap = null;
        return codeMapper.updateByPrimaryKeySelective(record);
    }
    
    public int updateByPrimaryKey(Code record) {
        codeMap = null;
        return codeMapper.updateByPrimaryKey(record);
    }

    public int deleteByExample(CodeExample example) {
        codeMap = null;
        return codeMapper.deleteByExample(example);
    }

    public int updateByExampleSelective(Code record, CodeExample example) {
        codeMap = null;
        return codeMapper.updateByExampleSelective(record, example);
    }

    public int updateByExample(Code record, CodeExample example) {
        codeMap = null;
        return codeMapper.updateByExample(record, example);
    }

    public int insert(Code record) {
        return codeMapper.insert(record);
    }

    public int insertSelective(Code record) {
        return codeMapper.insertSelective(record);
    }
    
    public String getCode(String code){
    	Code c = getByCode(code);
    	return c != null ? c.getValue() : null;
    }
    
    public Code getByCode(String code){
        freshMap();
    	return codeMap.get(code);
    }

    private void freshMap(){
        if (codeMap == null){
            codeMap = new HashMap<>();
            List<Code> all = this.codeMapper.selectByExample(new CodeExample());
            for (Code code : all){
                codeMap.put(code.getCode(),code);
            }
        }
    }

    public Integer getIntegerValue(String code){
        String value = getCode(code);
        return value == null?null:Integer.valueOf(value);
    }

    public Integer getFicNum(){
        Integer value = getIntegerValue(SystemConst.FICITIOUS_TOTAL);
        return value == null?20:value;
    }

    public Integer getRegularNum(){
        Integer value = getIntegerValue(SystemConst.REGULAR_TOTAL);
        return value == null?20:value;
    }
    public Integer getVrNum(){
        Integer value = getIntegerValue(SystemConst.VR_TOTAL);
        return value == null?10:value;
    }
    public Integer getStoreNum(){
        Integer value = getIntegerValue(SystemConst.STORE_TOTAL);
        return value;
    }
    public Integer getUserPushNum(){
        Integer value = getIntegerValue(SystemConst.USER_PUSH_TOTAL);
        return value == null?10:value;
    }

    public String getShareUrl(){
        String url = getCode(SystemConst.SHARE_URL);
        return url == null?ConstantsCode.SERVER_URL+"m/share":url;
    }

    public String getApiKey(){
        String isKey = getCode(SystemConst.app_api_verify);
        if ("1".equals(isKey)){
            String key = getCode(SystemConst.app_api_key);
            return StringUtil.isBlank(key)? SystemConst.MKM_API_KEY:key;
        }
        return null;
    }

    public Integer getUserHbNum(){
        Integer value = getIntegerValue(SystemConst.hb_day_num_limit);
        return value == null?1:value;
    }

    /**
     * 附近的人偷仓库倍数
     * @return
     */
    public Integer getNearMul(){
        String mul = getCode(SystemConst.user_near_catch);
        return StringUtil.isBlank(mul)?2:Integer.parseInt(mul);
    }

    /**
     * 宠物获取物品时间
     * @return
     */
    public Integer getPetTime() {
        Integer petTime = getIntegerValue(SystemConst.pet_time);
        return petTime == null?24:petTime;
    }


    public String getCreditRatio(Integer credit){
        if (credit == null){
            credit = 0;
        }
        Integer key = -1;
        for (int i = 0; i < MYjyConstant.credits.length; i++){
            if (credit <= MYjyConstant.credits[i]){
                key = i;
                break;
            }
        }
        if (key == -1){
            key = MYjyConstant.credits.length;
        }

        String code = "credit"+key;
        return getCode(code);
    }

    public String getCreditLevel(Integer credit){

        if(credit < 0){
            return "VIP0";
        }else if(credit >= 0 && credit <= 100){
            return "VIP1";
        }else if(credit > 100 && credit <= 400){
            return "VIP2";
        }else if(credit > 400 && credit <= 700){
            return "VIP3";
        }else if(credit > 700 && credit <= 1000){
            return "VIP4";
        }else if(credit > 1000){
            return "VIP5";
        }
        return "VIP0";
    }
}